// JavaScript Document
$(document).ready(function(e) {

	// calculate standard living fee
	standardLivingFee();
	
	// line-through the lower living expense
	rightExpense();

	// replace html texts
	/*textReplace('PropertyType','Type');
	textReplace('MortgageOwing','Owing');
	textReplace('MortgagePayment','Payment');
	textReplace('MortgageLender','Lender');
	textReplace('PropertyValue','Value');*/
	
	
	// hide empty tables
    $('.vehicle-table, .liability-table, .property-short-table').each(function(index, element) {
       var count = $(this).find('tr').length;
	   if(count<=1){
	   		$(this).hide();
	   }
    });
	
	
	//hide empty property tables
	$('.property-table tr:nth-child(2) td:nth-child(2)').each(function(index, element) {
		if($(this).text()==''){
			$(this).closest('table').hide();
		}
	});
	
	// color status texts
	$('.color-font').each(function(index, element) {
        var v=$(this).text();
		if(v=='Pass')
			$(this).css('color','green');
		if(v=='Manual Respond')
			$(this).css('color','rgb(255,165,0)');
		if(v=='Support Required')
			$(this).css('color','red');
    });
	
	
	// hide empty rows
	$("#personal-table td:nth-child(2)").each(function(index, element) {
        if($(this).text()==''){
			$(this).closest('tr').hide();	
		}
    });
	
	
	// find address in google map
	$('.address').click(function(e) {
        var v = $(this).text();
		googleAddress(v);
    });
	
	
	// open afterpage
	$('#afterpage').click(function(e) {
        var v = $(this).text(); 
		window.open(v);
    });
	
	
	// open company website
	$('#website').click(function(e) {
        var v = $(this).text(); 
		v = v.replace("http://", "");
		window.open("http://"+v);
    });
	
	
	// format DOB
	$('.dob').each(function(){
		if($(this).text()!='')
			$(this).formatDateTime('dd-M-yy');
	})
	
	
	// format liability table
	$('.liability-table tr td').each(function(index, element) {
   		var v = $(this).text();
		if(isNaN(v)==false){
			$(this).addClass('currency');
		}
	});
	
	
	// LVR calculation
	for(var i=1;i<=6;i++){
		var owing = $('#propertyOwing'+i).text();
		var value = $('#propertyValue'+i).text();
		getLVR(owing, value, i);
	}
	
	
	// Age calculation
	var dob1 = $('#dob1').text(); //alert (dob1); 
	var dob2 = $('#dob2').text(); //alert (dob2);
	displayAge(dob1, 1);
	displayAge(dob2, 2);
	
	
	
	// call formatCurrency plugin
	$('.currency').formatCurrency({ roundToDecimalPlace: 0, eventOnDecimalsEntered: true, negativeFormat: true });
	
	
	
	
	
	// line-through boardingrentingfee if own property
	/*if($('.property-table').is(':visible')){
		alert ('own property');
	 	$('.boarding').css('text-decoration','line-through');
	}*/
	
	//standardLivingFee();
	
});

function googleAddress(address){
	var url="https://maps.google.com.au/maps?oe=utf-8&q="+address;
	window.open(url);
}

function textReplace(textOld,textNew){
	var replaced = $("body").html().replace(textOld,textNew);
	$("body").html(replaced);
}

function rightExpense(){
	var d = $('#declared').text(); 	
	var s = $('#standard').text();  //alert (s);
	if(d > s)
		$('.standard').css('text-decoration','line-through');
	else
		$('.declared').css('text-decoration','line-through');
}

function standardLivingFee(){
	// Number of clients
	if($('#title2').text()!='')
		client = 2;
	else
		client = 1;
	
	var nod = $('#nod').text();
	
	if(client==1){
		if(nod==0) fee=990;	
		if(nod==1) fee=1224;
		if(nod==2) fee=1458;
		if(nod==3) fee=1692;
		if(nod==4) fee=1926;
		if(nod==5) fee=2161;
		if(nod==6) fee=2395;
	}else{
		if(nod==0) fee=1575;
		if(nod==1) fee=1809;
		if(nod==2) fee=2043;
		if(nod==3) fee=2278;
		if(nod==4) fee=2512;	
		if(nod==5) fee=2746;
		if(nod==6) fee=2980;	
	}
	
	$('#standard').html(fee);
}

function calculateAge (birthDate, otherDate) {
    birthDate = new Date(birthDate);
    otherDate = new Date(otherDate);

    var years = (otherDate.getFullYear() - birthDate.getFullYear());

    if (otherDate.getMonth() < birthDate.getMonth() || 
        otherDate.getMonth() == birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) {
        years--;
    }

    return years;
}

function GetCurrentDate() {
	// Today date time which will used to set as default date.
	var todayDate = new Date();

	todayDate = ("0" + todayDate.getDate()).slice(-2) + "/" +
				("0" + (todayDate.getMonth() + 1)).slice(-2)+ "/" +
				todayDate.getFullYear() ;
				 
	return todayDate;
}

function displayAge(dob, pos){
	//dob = dob.formatDateTime('dd/mm/yy'); //alert (dob);
	var todayDate = GetCurrentDate();
	var age = calculateAge(dob, todayDate);
	if(age){
		//dob = dob.formatDateTime('dd/M/yy');
		$('#age'+pos).html(' ('+age+' years old)');
		$('#dob'+pos).formatDateTime('dd-M-yy');
	}
}

function getLVR(owing, value, pos){
	var lvr = owing/value*100;
	lvr = lvr.toFixed(2); //alert (lvr);
	if(!isNaN(lvr))
		$('#LVR'+pos).html(lvr+'%');
}